package Tread;

import java.util.concurrent.CountDownLatch;

public class Example9_CountDownLatch {
    public static void main(String[] args) throws InterruptedException {
        int threadCount = 3;
        CountDownLatch latch = new CountDownLatch(threadCount);//需要等待 3 个线程。

        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + "完成任务");
                } finally {
                    latch.countDown();//使计数器减 1
                }
            }).start();
        }
        latch.await();//主线程在此 await()，直到 latch 的计数器变为 0 才会继续。
        System.out.println("all is over");
    }
}
